import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
		path: "/",
		redirect: "/home",
	}, {
		path: "/home",
		redirect: "/home/index",
	}, {
		path: "/home",
		name: "Home",
		meta: {
			show: true
		},
		component: () => import("../views/Home/Home.vue"),
		children: [{
			path: "index",
			name: "Home",
			meta: {
				show: true
			},
			component: () => import('../views/Home/children/index.vue')
		}, {
			path: "other",
			name: "Home",
			meta: {
				show: true
			},
			component: () => import('../views/Home/children/other.vue')
		}]
	}, {
		path: "/category",
		name: "Category",
		meta: {
			show: true
		},
		component: () => import("../views/Category/Category.vue"),
	}, {
		path: "/discover",
		name: "Discover",
		meta: {
			show: true
		},
		component: () => import("../views/Discover/Discover.vue"),
	}, {
		path: "/shopcart",
		name: "Shopcart",
		meta: {
			show: true
		},
		component: () => import("../views/Shopcart/Shopcart.vue"),
	}, {
		path: "/person",
		name: "Person",
		meta: {
			show: true
		},
		component: () => import("../views/Person/Person.vue"),
	}, {
		path: "/search",
		name: "Search",
		meta: {
			show: true
		},
		component: () => import("../views/Search/Search.vue"),
	},
	{
		path: '/login',
		name: 'Login',
		meta: {
			show: false
		},
		component: () => import('../views/Login/Login.vue')
	},
	{
		path: '/phonelogin',
		name: 'PhoneLogin',
		meta: {
			show: false
		},
		component: () => import('../views/Login/PhoneLogin.vue')
	}, {
		path: '/detail/:gid',
		name: 'Detail',
		meta: {
			show: false
		},
		component: () => import('../views/Detail/Detail.vue')
	},
	{
		path: '/categorylist/:catid', //动态路由
		name: 'Categorylist',
		meta: {
			show: false
		},
		component: () => import('../views/Category/components/Categorylist.vue')
	}
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes,
})
//路由守卫
router.beforeEach((to, from, next) => {
	//to: Route: 即将要进入的目标 路由对象
	//from: Route: 当前导航正要离开的路由
	//next: Function: 一定要调用该方法来 resolve 这个钩子。
	// console.log(to);
	// console.log(from);
	var token = window.localStorage.getItem("token")
	// console.log(token);
	if (to.path == "/person" || to.path == "/phonelogin" || to.path == "/login") {
		return next() //放行
	}
	if (!token) return next("/login")
	// console.log(111111111111);
	next()
})

export default router